/*
 * Copyright (C) 2021 The Chinese Software International Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tonyodev.fetch2.util;

import com.tonyodev.fetch2.CompletedDownload;
import com.tonyodev.fetch2.Download;
import com.tonyodev.fetch2.EnqueueAction;
import com.tonyodev.fetch2.Error;
import com.tonyodev.fetch2.NetworkType;
import com.tonyodev.fetch2.Priority;
import com.tonyodev.fetch2.Request;
import com.tonyodev.fetch2.Status;
import com.tonyodev.fetch2.database.DownloadInfo;
import com.tonyodev.fetch2core.FetchCoreUtils;

import org.jetbrains.annotations.NotNull;


/**
 * description TypeConverterExtensions
 *
 * @author liuluming
 * @since 2021-05-24
 */
public class TypeConverterExtensions {
    /**
     * toDownloadInfo
     *
     * @param request request
     * @param downloadInfo downloadInfo
     * @return true
     */
    public static DownloadInfo toDownloadInfo(@NotNull Request request, @NotNull DownloadInfo downloadInfo) {
        downloadInfo.setId(request.getId());
        downloadInfo.setUrl(request.getUrl());
        downloadInfo.setFile(request.getFile());
        downloadInfo.setPriority(request.getPriority());
        downloadInfo.setHeaders(request.getHeaders());
        downloadInfo.setGroup(request.getGroupId());
        downloadInfo.setNetworkType(request.getNetworkType());
        downloadInfo.setStatus(Defaults.getDefaultStatus());
        downloadInfo.setError(Defaults.getDefaultNoError());
        downloadInfo.setDownloaded(0L);
        downloadInfo.setTag(request.getTag());
        downloadInfo.setEnqueueAction(request.getEnqueueAction());
        downloadInfo.setIdentifier(request.getIdentifier());
        downloadInfo.setDownloadOnEnqueue(request.getDownloadOnEnqueue());
        downloadInfo.setExtras(request.getExtras());
        downloadInfo.setAutoRetryMaxAttempts(request.getAutoRetryMaxAttempts());
        downloadInfo.setAutoRetryAttempts(0);
        return downloadInfo;
    }

    /**
     * toDownloadInfo
     *
     * @param request request
     * @param downloadInfo downloadInfo
     * @return true
     */
    @NotNull
    public static DownloadInfo toDownloadInfo(@NotNull Download request, @NotNull DownloadInfo downloadInfo) {
        downloadInfo.setId(request.getId());
        downloadInfo.setNamespace(request.getNamespace());
        downloadInfo.setUrl(request.getUrl());
        downloadInfo.setFile(request.getFile());
        downloadInfo.setGroup(request.getGroup());
        downloadInfo.setPriority(request.getPriority());
        downloadInfo.setHeaders(request.getHeaders());
        downloadInfo.setDownloaded(request.getDownloaded());
        downloadInfo.setTotal(request.getTotal());
        downloadInfo.setStatus(request.getStatus());
        downloadInfo.setNetworkType(request.getNetworkType());
        downloadInfo.setError(request.getError());
        downloadInfo.setCreated(request.getCreated());
        downloadInfo.setTag(request.getTag());
        downloadInfo.setEnqueueAction(request.getEnqueueAction());
        downloadInfo.setIdentifier(request.getIdentifier());
        downloadInfo.setDownloadOnEnqueue(request.getDownloadOnEnqueue());
        downloadInfo.setExtras(request.getExtras());
        downloadInfo.setAutoRetryMaxAttempts(request.getAutoRetryMaxAttempts());
        downloadInfo.setAutoRetryAttempts(request.getAutoRetryAttempts());
        return downloadInfo;
    }

    /**
     * toDownloadInfo
     *
     * @param request request
     * @param downloadInfo downloadInfo
     * @return true
     */
    @NotNull
    public static DownloadInfo toDownloadInfo(@NotNull CompletedDownload request, @NotNull DownloadInfo downloadInfo) {
        downloadInfo.setId(FetchCoreUtils.getUniqueId(request.getUrl(), request.getFile()));
        downloadInfo.setUrl(request.getUrl());
        downloadInfo.setFile(request.getFile());
        downloadInfo.setGroup(request.getGroup());
        downloadInfo.setPriority(Priority.NORMAL);
        downloadInfo.setHeaders(request.getHeaders());
        downloadInfo.setDownloaded(request.getFileByteSize());
        downloadInfo.setTotal(request.getFileByteSize());
        downloadInfo.setStatus(Status.COMPLETED);
        downloadInfo.setNetworkType(NetworkType.ALL);
        downloadInfo.setError(Error.NONE);
        downloadInfo.setCreated(request.getCreated());
        downloadInfo.setTag(request.getTag());
        downloadInfo.setEnqueueAction(EnqueueAction.REPLACE_EXISTING);
        downloadInfo.setIdentifier(request.getIdentifier());
        downloadInfo.setDownloadOnEnqueue(true);
        downloadInfo.setExtras(request.getExtras());
        downloadInfo.setAutoRetryMaxAttempts(0);
        downloadInfo.setAutoRetryAttempts(0);
        return downloadInfo;
    }
}
